package com.example.thread.base.demo3;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/12/14 22:13
 *
 * 正确的停止线程
 */
@Slf4j
public class Demo3_1 {
    public static void main(String[] args) throws Exception {
        Runnable runnable = () -> {
            try {
                int count = 0;
                int countLimit = 1000;
                while (!Thread.currentThread().isInterrupted() && count < countLimit) {
                    System.out.println(count);
                    count++;
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                log.error("{}",e.getMessage(),e);
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
        Thread.sleep(5);
        thread.interrupt();
        //thread.resume();
    }
}
  //Thread.currentThread().interrupt();